package edu.nju.fs.action;

import org.apache.struts2.ServletActionContext;

import edu.nju.fs.daoImpl.AdminDaoImpl;
import edu.nju.fs.daoImpl.DConductorDaoImpl;
import edu.nju.fs.daoImpl.JConductorDaoImpl;
import edu.nju.fs.daoImpl.UserDaoImpl;
import edu.nju.fs.dao.AdminDao;
import edu.nju.fs.dao.DConductorDao;
import edu.nju.fs.dao.JConductorDao;
import edu.nju.fs.dao.UserDao;

public class CheckLoginAction {
	private String username;
	private String psw;
	private String type;
	private String message;
	
	private UserDao ud = new UserDaoImpl();
	private DConductorDao dcd = new DConductorDaoImpl();
	private JConductorDao jcd = new JConductorDaoImpl();
	private AdminDao ad = new AdminDaoImpl();
	
	public String execute(){
		String rmessage="fail";
		System.out.println(username+" "+psw+" "+type);
		if(type.equals("个人用户")){
			if(ud.isValidUser(username, psw)){
				ServletActionContext.getRequest().getSession()
				.setAttribute("user", ud.getUser(username));
				ServletActionContext.getRequest().getSession()
				.setAttribute("type", "个人用户");
				
				rmessage="user";
			}else{
				message="用户名不存在";
			}
			
		}else if(type.equals("机场售票员")){
			if(jcd.isValidUser(username, psw)){
				ServletActionContext.getRequest().getSession()
				.setAttribute("user", jcd.getUser(username));
				ServletActionContext.getRequest().getSession()
				.setAttribute("type", "机场售票员");
				
				rmessage="jconductor";
			}else{
				message="用户名不存在";
			}
		}else if(type.equals("代售点售票员")){
			if(dcd.isValidUser(username, psw)){
				ServletActionContext.getRequest().getSession()
				.setAttribute("user", dcd.getUser(username));
				ServletActionContext.getRequest().getSession()
				.setAttribute("type", "代售点售票员");
				
				rmessage="dconductor";
			}else{
				message="用户名不存在";
			}
		}else if(type.equals("管理员")){
			if(ad.isValidUser(username, psw)){
				ServletActionContext.getRequest().getSession()
				.setAttribute("user", ad.getUser(username));
				ServletActionContext.getRequest().getSession()
				.setAttribute("type", "管理员");
				
				rmessage="admin";
			}else{
				message="用户名不存在";
			}
		}
		
		return rmessage;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPsw() {
		return psw;
	}

	public void setPsw(String psw) {
		this.psw = psw;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	
}
